package com.mkvld.appietijdenv2.data;

import java.util.ArrayList;
import java.util.List;

import com.mkvld.appietijdenv2.databases.UserBudgetWithdrawalDatabase;

import android.content.Context;
import android.content.SharedPreferences;

public class PeriodBudget {
	public static List<PeriodBudget> budgets;
	
	private int contract, year, period;
	private double worked;
	
	private Budget advBudget, vacationBudget, tvtBudget;

	private Double advWithdrawn, vacationWithdrawn, tvtWithdrawn;
	
	public PeriodBudget(int contract, int year, int period, 
			Budget advBudget, Budget vacationBudget, Budget tvtBudget) {
		this.contract = contract;
		this.year = year;
		this.period = period;
		this.advBudget = advBudget;
		this.vacationBudget = vacationBudget;
		this.tvtBudget = tvtBudget;
	}
	
	public PeriodBudget(PeriodBudget old, double worked) {
		contract = old.contract;
		
		period = old.period;
		year = old.year;
		if (++period == 13) {
			period = 0;
			year++;
		}
		
		this.worked = worked;
		
		advBudget = new Budget(old.advBudget);
		vacationBudget = new Budget(old.vacationBudget);
		tvtBudget = new Budget(old.tvtBudget);
		
		// get custom withdrawals
		advWithdrawn = UserBudgetWithdrawalDatabase.wDb.getADVWithdrawal(year, period);
		vacationWithdrawn = UserBudgetWithdrawalDatabase.wDb.getVacationWithdrawal(year, period);
		tvtWithdrawn = UserBudgetWithdrawalDatabase.wDb.getTVTWithdrawal(year, period);
		
		calculateWithdrawals();
				
		// calculate newly aquired budgets
		double total = worked + advWithdrawn + vacationWithdrawn + tvtWithdrawn;
		advBudget.breeded += 0.075 * total;
		if (period <= 6)
			vacationBudget.breeded += 1.2/13 * total;
		else
			vacationBudget.nextYear += 1.2/13 * total;
	}
	
	
	
	public void calculateWithdrawals() {
		double shortage = contract - worked; 
		if (tvtWithdrawn > -1)
			shortage -= tvtWithdrawn;
		if (advWithdrawn > -1)
			shortage -= advWithdrawn;
		if (vacationWithdrawn > -1)
			shortage -= vacationWithdrawn;
		if (worked < contract) {
			if (tvtWithdrawn == -1) {
				double tvtWithdrawable = tvtBudget.getWithdrawable(period, contract);
				if (tvtWithdrawable >= shortage) {
					tvtWithdrawn = shortage;
					return;
				} else {
					tvtWithdrawn = tvtWithdrawable;
					shortage -= tvtWithdrawn;
				}
			}
			if (advWithdrawn == -1) {
				double advWithdrawable = advBudget.getWithdrawable(period, contract);
				if (advWithdrawable >= shortage) {
					advWithdrawn = shortage;
					return;
				} else {
					advWithdrawn = advWithdrawable;
					shortage -= advWithdrawn;
				}
			}
			if (vacationWithdrawn == -1) {
				double vacationWithdrawable = vacationBudget.getWithdrawable(period, contract);
				if (vacationWithdrawable >= shortage) {
					vacationWithdrawn = shortage;
				} else {
					vacationWithdrawn = vacationWithdrawable;
					shortage -= vacationWithdrawn;
				}
			}
		}
	}	
	
	public static void initPeriodBudgetList(Context context) {
		budgets = new ArrayList<PeriodBudget>();
		
		SharedPreferences prefs = Preferences.get(context);
		int year = Integer.parseInt(prefs.getString("budget_year", "0"));
		int period = Integer.parseInt(prefs.getString("budget_period", "0"));
		Budget vacationBudget = new Budget(Budget.TYPE_VACATION, 
				Double.parseDouble(prefs.getString("budget_vacation_budget", "0")), 
				Double.parseDouble(prefs.getString("budget_vacation_breeded", "0")), 
				Double.parseDouble(prefs.getString("budget_vacation_withdrawn", "0")), 
				Double.parseDouble(prefs.getString("budget_vacation_nextyear", "0")));
		Budget advBudget = new Budget(Budget.TYPE_ADV, 
				Double.parseDouble(prefs.getString("budget_adv_budget", "0")), 
				Double.parseDouble(prefs.getString("budget_adv_breeded", "0")), 
				Double.parseDouble(prefs.getString("budget_adv_withdrawn", "0")), 
				0);
		Budget tvtBudget = new Budget(
				Budget.TYPE_TVT, 
				Double.parseDouble(prefs.getString("budget_tvt_budget", "0")), 
				Double.parseDouble(prefs.getString("budget_tvt_breeded", "0")), 
				Double.parseDouble(prefs.getString("budget_tvt_withdrawn", "0")), 
				0);
		budgets.add(new PeriodBudget(Preferences.getContract(context), 
				year, period, advBudget, vacationBudget, tvtBudget));
		
	}
}
